<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    /*
        1.基础
            - 不传值返回undefined
     */
    let p = new Promise(() => {
    })
    let p2 = new Promise(resolve => resolve('666'))
    // let p3 = new Promise((resolve, reject) => reject('bad'))
    setTimeout(console.log, 0, p)  // Promise{<pending>}
    console.log(p2)  // Promise{<fulfilled>:666}
    // console.log(p3)  // Promise{<reject>:bad}   并抛出错误

    /*
        2.promise状态机,一旦改变,不可逆
            - pending
            - fulfilled  也成为resolved
            - rejected
     */

    /*
        3.执行器函数是同步执行的
     */
    let p4 = new Promise((resolve, reject) => setTimeout(resolve, 1000));
    // 在console.log打印期约实例的时候，还不会执行超时回调(即resolve()) 
    setTimeout(console.log, 0, p4); // Promise <pending>
    
    /*
        4.Promise.resolve()
     */
    
    // 4.2 这个是相同的
    let pro1 = new Promise(resolve => resolve(1))
    // 只会取第一个参数
    let pro2 = Promise.resolve(4,5,7);
    console.log('pro2',pro2)
    
    // 4.3 Promise.resolve()是一个幂等函数
    let p4_3 = Promise.resolve();
    console.log('幂等',p4_3 === Promise.resolve(p4_3))

    /*
        5.promise.reject()
     */
    let p5_1 = new Promise(((resolve, reject) => reject(2)))
    setTimeout(console.log,0,'p5_1',p5_1)
    p5_1.then(null,(e) => setTimeout(console.log,0,'5.捕获错误',e))

    
    /*
        6.同步异步的执行的二元性
            - 不能用try-catch捕获,需要用then的catch捕获
     */
    
    
</script>
</body>
</html>